package net.news.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.catalina.connector.Request;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import net.news.entity.News;
import net.news.entity.NewsPeople;
import net.news.entity.NewsType;
import net.news.service.NewsService;
import net.news.service.NewsTypeService;

import com.liesp.util.ConstantUtils;
import com.liesp.util.PropertyFilter;
import com.liesp.util.StringUtil;
import com.livesky.framework.miniapp.ActionContext;
import com.livesky.framework.miniapp.ActionSupport;
import com.livesky.framework.miniapp.utils.ViewUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * 项目名称：cmunews3084<br>
 * 类名称：newsAction <br>
 * 类描述： TODO(请输入类的描述) <br>
 * 创建人：charles 创建时间：2011-5-8 下午11:09:25 <br>
 * 修改人： 修改时间： <br>
 * 修改备注：
 * 
 * @version V1.0
 */
@Controller
public class NewsViewAction extends ActionSupport {
	private News news = new News();
	private String newsid;
	private NewsType newsType = new NewsType();
	private List<News> newsList;
	private List<NewsType> newsTypes;
	@Resource
	private NewsService newsService;
	@Resource
	private NewsTypeService newsTypeService;

	/**
	 *@author charles 2011-05-08 23:11 保存新闻方法
	 * @return
	 */
	public String addnews() {
		if(news.getNewsTitle()!=null&&news.getNewsTitle().length()>0){
			System.out.println("dddd");
			NewsPeople newsPeople = (NewsPeople) ActionContext.getContext()
					.getSession().getAttribute(ConstantUtils.USER_LOGIN_SESSIOIN);
			newsService.addnews(news, newsPeople, newsType);
			return "r:news_list";
		}
		ViewUtils.renderScript("alert('请输入标题！');history.back()");
		return null;
	}

	/**
	 * 新闻显示列表
	 * 
	 * @return
	 */
	public String newsList() {
		List<PropertyFilter> filters=PropertyFilter.buildFromHttpRequest(ViewUtils.getRequest());
		newsList = newsService.getNewsByFilter(filters);
		newsTypes=newsTypeService.getNewstype();
		return SUCCESS;
	}

	/**
	 * 修改新闻的视图
	 * 
	 * @return
	 */
	public String editview() {
		news = newsService.getNewsById(news);
		newsTypes = newsTypeService.getNewstype();
		return SUCCESS;
	}

	/**
	 * 修改新闻更新方法
	 * 
	 * @return
	 */
	public String editnews() {
		if(news.getNewsTitle()!=null&&news.getNewsTitle().length()>0){
			NewsPeople newsPeople = (NewsPeople) ActionContext.getContext()
			.getSession().getAttribute(ConstantUtils.USER_LOGIN_SESSIOIN);
			newsService.modifyNews(news, newsPeople, newsType);
			return "r:news_list";
		}
		ViewUtils.renderScript("alert('请输入标题！');history.back()");
		return null;
	}

	/**
	 * 新闻详细页面显示方法
	 * 
	 * @return
	 */
	public String newsinfo() {
		news = newsService.getNewsById(news);
		return SUCCESS;
	}
	
	/**新闻删除方法
	 * @return
	 */
	public String deleteNews(){
		newsService.deleteNews(news);
		return "r:news_list";
	}
	/**
	 * @return the news
	 */
	public News getNews() {
		return news;
	}

	/**
	 * @param news
	 *            the news to set
	 */
	public void setNews(News news) {
		this.news = news;
	}

	/**
	 * @return the newsType
	 */
	public NewsType getNewsType() {
		return newsType;
	}

	/**
	 * @param newsType
	 *            the newsType to set
	 */
	public void setNewsType(NewsType newsType) {
		this.newsType = newsType;
	}

	/**
	 * @return the newsList
	 */
	public List<News> getNewsList() {
		return newsList;
	}

	/**
	 * @param newsList
	 *            the newsList to set
	 */
	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	/**
	 * @return the newsTypes
	 */
	public List<NewsType> getNewsTypes() {
		return newsTypes;
	}

	/**
	 * @param newsTypes
	 *            the newsTypes to set
	 */
	public void setNewsTypes(List<NewsType> newsTypes) {
		this.newsTypes = newsTypes;
	}

	/**
	 * @return the newsid
	 */
	public String getNewsid() {
		return newsid;
	}

	/**
	 * @param newsid the newsid to set
	 */
	public void setNewsid(String newsid) {
		this.newsid = newsid;
	}

}
